# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:张广勤
@Web site: https://www.tunan.wang
@Github:www.github.com
 
@File:myApp.py
@Time:2024/3/5 20:53

@Motto:不积跬步无以至千里，不积小流无以成江海！
"""

import streamlit as st
import pandas as pd
from literature import random_line1_0
import csv


# st.set_page_config(page_title="Tunan",page_icon=r'.\static\favicon.ico')
df = pd.DataFrame({
  'first column': ['中国文学常识','中国文学常识关键字查找'],
  'second column': [13, 14]
})

# df
option = st.sidebar.selectbox(
    'Which number do you like best?',
     df['first column'])

# st.markdown('<p style="text-align:right;">'+raddom_quote.get_random_quote()+'</p>',unsafe_allow_html=True)
st.markdown('<p style="text-align:right;">' + random_line1_0.random_line("./static/caigentan.md") + '</p>', unsafe_allow_html=True)
'You selected:', option


if option == '中国文学常识':
    with open('./baidu_baike/china_num.csv', 'r', newline='', encoding='utf-8') as csvfile:
        reader = csv.reader(csvfile)
        # 循环遍历CSV文件的每一行
        for row in reader:
            elements = []
            links = []
            # print(f"这一行有{len(row)}项")
            # 如果行中有至少两个元素（索引从0开始，所以第二个元素的索引是1）
            if len(row) > 1:
                for i in range(0,len(row)):
                    elements.append(row[i])
                    links.append(f"https://baike.baidu.com/item/"+row[i])
                    merged_list = [(a, b) for a, b in zip(elements, links)]
                    # st.markdown('<a href="https://baike.baidu.com/item/'+row[i]+'">'+row[i]+'</a>',unsafe_allow_html=True)
                markdown_text = "- " + " | ".join([f"[{text}]({url})" for text, url in merged_list])
                st.markdown(markdown_text)

if option == '中国文学常识关键字查找':
    with open('./baidu_baike/china_num.csv', 'r', newline='', encoding='utf-8') as csvfile:
        reader = csv.reader(csvfile)
        # print(reader)
        search_content = st.text_input('输入要查找的关键字：', '五')
        search_result = []
        # for row_num, row in enumerate(reader, start=1):
        for row in reader:
            # 将行内容转化为字符串，然后检查是否包含特定内容
            # if search_content in ' '.join(row):
            if row and search_content in row[0]:
                # print(f"{row}")
                search_result.append(row)

    for row in search_result:
        elements = []
        links = []
        if len(row) > 1:
            for i in range(0, len(row)):
                elements.append(row[i])
                links.append(f"https://baike.baidu.com/item/" + row[i])
                merged_list = [(a, b) for a, b in zip(elements, links)]
                # st.markdown('<a href="https://baike.baidu.com/item/'+row[i]+'">'+row[i]+'</a>',unsafe_allow_html=True)
            markdown_text = "- " + " | ".join([f"[{text}]({url})" for text, url in merged_list])
            st.markdown(markdown_text)

